﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.SearchEntity;
using LY.Entity.PageSearch;

namespace LY.DataAccess.Repository
{
    public class DictRepository : BaseRepository
    {
        #region 字典
        /// <summary>
        /// 字典目录列表
        /// </summary>
        /// <returns></returns>
        public List<S_DictDir> GetDictDirList()
        {
            string sql = @" SELECT *
                       FROM [S_DictDir] ";
            return base.Query<S_DictDir>(sql,null);
        }

        /// <summary>
        /// 字典目录列表分页
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<S_DictDir> QueryDictDirList(DictDirSearch search, PageView view)
        {

            string cols = @" * ";
            string sqlWhere = "";
            if (!string.IsNullOrEmpty(search.Name))
            {
                sqlWhere += " AND (Name LIKE '%" + search.Name + "%')";
            }

            return base.PageGet<S_DictDir>(view, cols,
                @"[S_DictDir] "
                , sqlWhere
                , "ID DESC ",
                "");
        }

        /// <summary>
        /// 字典目录实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public S_DictDir GetDictDir(int? id)
        {
            string sql = " SELECT * FROM S_DictDir WHERE ID=@ID";
            return base.Get<S_DictDir>(sql, new { ID = id });
        }
        
        /// <summary>
        /// 验证字典目录是否有重复
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public int CheckDictDir(string Name)
        {
            string sql = "SELECT COUNT(ID) FROM S_DictDir WHERE Name=@Name";
            return base.Get<int>(sql, new { Name = Name });
        }

        /// <summary>
        /// 删除字典目录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDictDir(int? id)
        {
            string sql = " DELETE  FROM S_DictDir WHERE ID=@ID";
            return base.Excute(sql, new { ID = id });
        }
        #endregion

        #region 字典项
        /// <summary>
        /// 字典项列表
        /// </summary>
        /// <returns></returns>
        public List<S_DictItem> GetDictItemList()
        {
            string sql = @" SELECT *
                       FROM [S_DictItem] ";
            return base.Query<S_DictItem>(sql, null);
        }

        /// <summary>
        /// 字典项列表分页
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<S_DictItem> QueryDictItemList(DictItemSearch search, PageView view)
        {

            string cols = @" * ";
            string sqlWhere = "";
            if (!string.IsNullOrEmpty(search.Name))
            {
                sqlWhere += " AND (Name LIKE '%" + search.Name + "%')";
            }
            if (!string.IsNullOrEmpty(search.DictDirId))
            {
                sqlWhere += " AND DirID = " + search.DictDirId + " ";
            }

            return base.PageGet<S_DictItem>(view, cols,
                @"[S_DictItem] "
                , sqlWhere
                , "ID DESC ",
                "");
        }

        /// <summary>
        /// 字典项实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public S_DictItem GetDictItem(int? id)
        {
            string sql = " SELECT * FROM S_DictItem WHERE ID=@ID";
            return base.Get<S_DictItem>(sql, new { ID = id });
        }

        /// <summary>
        /// 验证字典项是否有重复
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public int CheckDictItem(string Name)
        {
            string sql = "SELECT COUNT(ID) FROM S_DictItem WHERE Name=@Name";

            return base.Get<int>(sql, new { Name = Name });
        }

        /// <summary>
        /// 删除字典目录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDictItem(int? id)
        {
            string sql = " DELETE  FROM S_DictItem WHERE ID=@ID";
            return base.Excute(sql, new { ID = id });
        }
        #endregion
    }
}
